import {Component} from "@angular/core";
import {ActionSheetController, AlertController, IonicPage, NavController, NavParams} from "ionic-angular";
import {Storage} from "@ionic/storage";
import {CashdetailPage} from "../business-food-cashdetail/business-food-cashdetail";
import {HttpClient} from "@angular/common/http";
import {AppUrl} from "../../../app/app.general";

@IonicPage()
@Component({
  templateUrl: 'business-food-cash.html',
  selector: 'business-food-cash'
})

export class BusinessFoodCashPage {

  goodData: Array<{ floor: number,
    orderTable: Array<{ tableid: number, tablename: string,sumPrice:any,orderno:string}>
  }>;
  theFloor: any;
  floor: number;
  floorNo: any=[];

  sliceflag:number;

  constructor(public navCtrl: NavController, public navParams: NavParams, private http:HttpClient, public storage: Storage) {
    this.goodData=[];
    this.http.post(AppUrl.food.cashtable,{}).subscribe(data=>{
      let temp1:any=data;
      let temp:any[]=temp1.data;

      for(let ar of temp){
        let index=this.floorNo.findIndex(e=>e.no==ar.floorid);
        if(index!=-1){
          let k=this.goodData.findIndex(e=>e.floor==ar.floorid);
          this.goodData[k].orderTable.push({
            tableid:ar.id,
            tablename: ar.tableno,
            sumPrice:ar.totalprice,
            orderno:ar.orderno
          });
        }else{
          this.floorNo.push({no:ar.floorid,name:ar.floor});
          let k=this.goodData.findIndex(e=>e.floor==ar.floorid);
          if(k!=-1){
            this.goodData[k].orderTable.push({
              tableid:ar.id,
              tablename: ar.tableno,
              sumPrice:ar.totalprice,
              orderno:ar.orderno
            });
          }else{
            this.goodData.push({
              floor:ar.floorid,
              orderTable:[{
                tableid:ar.id,
                tablename: ar.tableno,
                sumPrice:ar.totalprice,
                orderno:ar.orderno
              }],
            });
          }
        }
      }
      if(this.floorNo.length>0){
        this.theFloor = this.floorNo[0].no;
      }
      console.log(this.theFloor);
      console.log(this.goodData);
    });
  }

  segmentChanged(data){
    this.theFloor=data;
  }
  cashDetail(item,floor,i) {
    this.sliceflag=i+1;
    console.log(i);
    this.navCtrl.push('CashdetailPage', {data:item,floor:floor,pagetitle:'收银',callback:this.getdata});
  }

  getdata=(data)=>{
    return new Promise((resolve,reject)=>{
      console.log(data);
      let index=this.goodData.findIndex(e=>e.floor==data.floor);
      console.log(index);
      this.goodData[index].orderTable.splice(this.sliceflag-1,1);
      resolve();
    });
  }
}
